package com.example.demo.annotation;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author liuhe
 * @date 2020/08/25
 */
public class Jdk8Test {

    public static void main(String[] args) {

//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                System.out.println("before jdk8,too much code");
//            }
//        }).start();
//        new Thread(() -> System.out.println("after jdk8,lambda simple code")).start();

//        List<String> strings = Arrays.asList("lambda", "default method", "stream api");
//        for (String str : strings) {
//            System.out.println(str);
//        }
//
//        strings.forEach((str) -> {
//            System.out.println(str);
//        });

        List<Integer> costTax = Arrays.asList(100, 200, 300, 200, 400);
//        Double doubleStream = costTax.stream().map((cost) -> cost + cost * 0.12).reduce((sum, cost)->sum+cost).get();
//        System.out.println(doubleStream);


//        List<Integer> collect = costTax.stream().map((cost) -> cost * 2).distinct().collect(Collectors.toList());
//        System.out.println(collect);
//
//
//        IntStream.range(0,5).forEach(i-> System.out.println(i));

//        Stream.of("a1","b2","c3")
//                .filter(s->{
//                    System.out.println("filter:"+s);
//                    return s.startsWith("b");
//                })
//                .map(s->{
//                    System.out.println("map:" + s);
//                    return s.toUpperCase();
//                })
//                .forEach(s->{
//                    System.out.println("forEach:" + s);
//                });

//        List<String> collect = Stream.of("a1", "b2", "c3")
//                .filter(s -> {
//                    System.out.println("filter:" + s);
//                    return s.startsWith("b");
//                })
//                .map(s -> {
//                    System.out.println("map:" + s);
//                    return s.toUpperCase();
//                })
//                .collect(Collectors.toList());
//        System.out.println(collect);

//        Stream.of("apple", "banana", "orange", "waltermaleon", "grape")
//                .map(e -> e.length()) //转成单词的长度 int
//                .sorted()
//                .forEach(e -> System.out.println(e)); //输出

        List<String> collect = Stream.of("bbab", "banana", "orange", "waltermaleon", "grape")
                .filter(it -> it.startsWith("b"))
                .collect(Collectors.toList());

        String first = Stream.of("bbab", "grape")
                .findFirst()
                .orElse("b");
        System.out.println(first);


        List<Integer> numbers = Arrays.asList(9, 10, 3, 4, 23, 2);
        List<Integer> collect1 = numbers.stream().map(i -> i * i).distinct().collect(Collectors.toList());
        System.out.println(numbers);
        System.out.println(collect1);
    }
}
